package cc.atliberty.structural.composite.ex2;

import java.util.Enumeration;
import java.util.Vector;

/**
 * @Author 元宵
 * @url www.atliberty.cc
 * @Date 2023-10-31 08:43
 * @description:
 */
public class Composite implements Component{
    private Vector componentVector = new Vector();
    
    public Composite getComposite() {
        return this;
    }
    
    public void sampleOperation() {
        Enumeration enumeration = components();
        while (enumeration.hasMoreElements()) {
            ((Component)enumeration.nextElement()).sampleOperation();
        }
    }
    
    public void add(Component component) {
        componentVector.addElement(component);
    }

    public void  remove(Component component) {
        componentVector.removeElement(component);
    }
    private Enumeration components() {
        return componentVector.elements();
    }
}
